package com.fy.module.repo.controller;

import com.fy.common.dto.PageDto;
import com.fy.common.dto.PageResult;
import com.fy.common.exception.ServiceException;
import com.fy.common.vo.ApiRest;
import com.fy.module.repo.entity.Repo;
import com.fy.module.repo.service.IRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/repo")
@Api(description = "题库管理")
@CrossOrigin
public class RepoController {

    @Autowired
    private IRepoService repoService;

    @GetMapping("list")
    @ApiOperation("查询所有的题库")
    @PreAuthorize("hasAnyAuthority('student_add','student_list','course_list','course_add')")
    public List<Repo> findAll(){
        System.out.println("111");
        System.out.println(222);
        return  repoService.list();
    }

    @ApiOperation("分页查询题库")
    @PostMapping("/page")
    @PreAuthorize("hasAnyAuthority('student_add','student_list','course_list','course_add')")
    public PageResult getRepoByPage(@RequestBody PageDto<Repo> pageDto){
        PageResult<Repo> repoPageResult = repoService.selectRepoByPages(pageDto);
        return repoPageResult;
    }





    /**
     * 删除单个题库
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除题库")
    @PreAuthorize("hasAnyAuthority('student_add','student_list','course_list','course_add')")
    public ApiRest deleteById(@PathVariable("id") @ApiParam("题库id") Integer id) {
        if (id == null) {
            throw new ServiceException("删除id参数异常");
        }
        boolean b = repoService.removeById(id);
        if (b) {
            return ApiRest.success("删除成功",null);
        }

        return ApiRest.failure("单个id删除失败,没有这个id");
    }

    /**
     * 删除全部题库
     */

    @DeleteMapping("/deleteAll")
    @ApiOperation("批量删除题库")
    @PreAuthorize("hasAnyAuthority('student_add','student_list','course_list','course_add')")
    public ApiRest deleteAll(@RequestBody List<String> deleteIds){
        if (deleteIds == null) {
            throw new ServiceException("删除多个id参数异常");
        }
        boolean b = repoService.removeByIds(deleteIds);
        if (b) {
            return ApiRest.success("删除多个成功", null);
        }
        return ApiRest.failure("多个id删除失败,参数异常");
    }

    /**
     * 添加题库
     */
    @PostMapping("/add")
    @ApiOperation("添加题库")
    @PreAuthorize("hasAnyAuthority('student_add','student_list','course_list','course_add')")
    public ApiRest addRepo(@RequestBody Repo repo){
        ApiRest apiRest = repoService.insertRepo(repo);
        return apiRest;
    }


    /**
     * 修改题库
     */
    @PutMapping("/update")
    @ApiOperation("修改题库")
    @PreAuthorize("hasAnyAuthority('student_add','student_list','course_list','course_add')")
    public ApiRest updateRepo(@RequestBody @ApiParam("更新的参数") Repo repo) {
        if (repo == null || repo.getId() == null){
            throw new ServiceException("修改参数异常");
        }
        boolean b = repoService.updateById(repo);
        if (b) {
            return ApiRest.success("修改成功", null);
        }
        return ApiRest.failure("修改失败");
    }


}
